import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';

/// 多选drawer约束
abstract class AbstractRequestFormMultipleSelectDrawer {
  final BaseHttpRequestModel request;

  final dynamic dataSource;

  final VoidCallback onClose;

  final Function(List<String> keys, List<dynamic> values) onSave;

  AbstractRequestFormMultipleSelectDrawer(this.request, this.dataSource, this.onClose, this.onSave);
}

/// 基本多选
class RequestFormMultipleSelectDrawer extends StatefulWidget implements AbstractRequestFormMultipleSelectDrawer {
  RequestFormMultipleSelectDrawer({Key key, @required this.request, this.dataSource, this.onClose, this.onSave}) : super(key: key);

  final BaseHttpRequestModel request;

  final dynamic dataSource;

  final VoidCallback onClose;

  final Function(List<String> keys, List<dynamic> values) onSave;

  @override
  _RequestFormMultipleSelectDrawerState createState() => _RequestFormMultipleSelectDrawerState();
}

class _RequestFormMultipleSelectDrawerState extends State<RequestFormMultipleSelectDrawer> {
  String _service;

  List<String> _keys;

  List<dynamic> _values;

  @override
  void initState() {
    this._service = BaseTransformUtil.matchServiceUrl(widget.request.inputApi, widget.dataSource);
    this._keys = widget.dataSource == null ? [] : widget.dataSource[widget.request.fieldName] ?? [];
    String value = BaseUtils.getKeyData(widget.dataSource, '${widget.request.fieldName}Name').toString();
    this._values = value.length == 0 ? [] : value.split(',');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: BaseRefreshComponent(
            path: _service,
            method: BaseHttpMethodEnum.GET,
            buildItem: (Map<String, dynamic> item, int index) => _buildRadioListItem(context, item, index),
            isPaging: false,
          ),
        ),
        this._buildDrawerBottomButton(),
      ],
    );
  }

  Widget _buildRadioListItem(BuildContext context, Map<String, dynamic> item, int index) {
    String key = BaseUtils.getKeyData(item, 'tagId').toString();
    String value = BaseUtils.getKeyData(item, 'tagValue').toString();
    return Container(
      decoration: new BoxDecoration(
          border: Border(
        bottom: BorderSide(color: Theme.of(context).scaffoldBackgroundColor),
      )),
      child: CheckboxListTile(
        title: Text(value, style: Theme.of(context).textTheme.bodyText2),
        value: this._keys.contains(key),
        onChanged: (bool selected) {
          setState(() {
            selected == true ? this._keys.add(key) : this._keys.remove(key);
            selected == true ? this._values.add(value) : this._values.remove(value);
          });
        },
      ),
    );
  }

  Widget _buildDrawerBottomButton() {
    return BaseDrawerBottom(
      close: widget.onClose,
      sure: () {
        widget.onSave(this._keys, this._values);
      },
    );
  }
}
